@extends('layouts/app')

@section('content')
    <div class="weui-form-preview">
        <div class="weui-form-preview__hd">
            <label class="weui-form-preview__label payMoney">付款金额：</label>
            <em class="weui-form-preview__value_money money"></em>
        </div>
        <div class="weui-form-preview__bd">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label balanceName"></label>
                <span class="weui-form-preview__value balance"></span>
            </div>
        </div>
        <div class="weui-form-preview__bd">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label ">总计：</label>
                <span class="weui-form-preview__value orderPrice"></span>
            </div>
        </div>
        <div class="weui-form-preview__bd">
            <div class="weui-form-preview__item">
                <label class="weui-form-preview__label">订单编号：</label>
                <span class="weui-form-preview__value orderNum"></span>
            </div>
        </div>

    </div>

    <div class="weui-cells weui-cells_radio">
        <label class="weui-cell weui-check__label zhanglitong" for="x11">
            <div class="weui-cell__bd">
                <p>余额支付</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" value="zhanglitong" name="pay_way" id="x11" checked="checked">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="x12">

            <div class="weui-cell__bd">
                <p>微信支付</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" value="wechat" name="pay_way" class="weui-check" id="x12">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label alipay" for="x13">

            <div class="weui-cell__bd">
                <p>支付宝支付</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" value="alipay" name="pay_way" class="weui-check" id="x13">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
    </div>

    <div class="demos-content-padded">
        <a href="javascript:;" class="weui-btn weui-btn_primary f30" onclick="doPay()">确认支付</a>
    </div>

    {{--手机普通览器支付专用表单--}}
    <form action="/api/pay" method="post" name="info" id="info">
        {{--传参pay_way=wechat，order_id=订单id,pay_port=web--}}
        <input type="hidden" name="pay_way" value="wechat">
        <input type="hidden" name="order_id">
        <input type="hidden" name="pay_port" value="web">
    </form>

    {{--手机普通览器支付专用表单--}}
    <form action="/api/payh5" method="get" name="winfo" id="winfo">
        {{--传参pay_way=wechat，order_id=订单id,pay_port=web--}}
        <input type="hidden" name="pay_way" value="wechatapp">
        <input type="hidden" name="order_id">
        {{--<input type="hidden" name="pay_port" value="web">--}}
    </form>

    {{--支付宝专用表单--}}
    <form action="/api/pay" method="post" name="ainfo" id="ainfo">
        {{--传参pay_way=wechat，order_id=订单id,pay_port=web--}}
        <input type="hidden" name="pay_way" value="alipay">
        <input type="hidden" name="order_id">
        <input type="hidden" name="pay_port" value="web">
    </form>

    {{--// 扫码支付,二维码容器--}}
    <div id="code" class="weui-popup__container">
        <div class="weui-popup__modal">
            <div class="img" id="img">
                {{--<img src="/img/eat.png" alt="" class="img-responsive">--}}
            </div>
            <div class="info text-center"></div>
            <div class="btn padding">
                <button class="weui-btn weui-btn_primary close-popup f30" data-target="code"> 关闭</button>
            </div>
        </div>
    </div>


    <script src="/qrcodejs-master/qrcode.js"></script>
    <script>
        var _token = $("input[name='_token']").val();
        // 如果没登录，隐藏全额支付
        getUserInfo(_token,function (info) {
            console.log("info", info);
            if (!info[0].id) {
                $(".zhanglitong").hide();
            }
        });

        // 在手机 微信浏览器把支付宝支付隐藏掉
        var isWb = isWechatBrowser();
        if (isWb) {
            $(".alipay").hide();
        }

        var id = getRequest().id;
        $.post("/api/check_order", {id: id}, function (res) {
            if (typeof res === 'string') {
                res = JSON.parse(res);
            }
            if (res.code != 200) {
                if(res.code==201 || res.code==203){
                    window.location.href="/mobile/index";
                }
                window.location.href="/mobile/order-detail?id=" + id;
            }
            order_type = res.data.order_type
            order_number = res.data.order_number

            if(order_type ==0 || order_type ==3 ){//外卖//扫码
                money = res.data.order_amount
                balance = res.data.order_price - res.data.order_amount
                if(balance>0){
                    $(".balanceName").text('优惠金额：');
                    $(".balance").text('￥' + parseFloat(balance).toFixed(2));
                }
                orderPrice =  res.data.order_price
            }else if(order_type ==1){//预定
                if( res.data.order_status==3){
                    money = res.data.order_price-res.data.order_amount
                    if(money<0){
                        alert('未达最低消费不可结账');
                        history.back();
                    }
                    $(".balanceName").text('已付定金：');
                    $(".balance").text('￥' + parseFloat(res.data.order_amount).toFixed(2));
                }else{
                    money = res.data.order_amount
                }
                orderPrice = res.data.order_price
            } else{
                money = res.data.order_amount
                orderPrice = res.data.order_amount
            }
            if(money<0){
                $.toast('订单金额有误，请联系商家', 'text');
            }
            $(".money").text('￥' + parseFloat(money).toFixed(2));
            $(".orderPrice").text('￥' + parseFloat(orderPrice).toFixed(2));
            $(".orderNum").text(order_number);
            $("input[name='order_id']").val(id);
            if (order_type == 5) {
                $(".zhanglitong").hide();
                $("#x12").prop("checked", true);
            }
        });
        //监听订单
        function checkOrder () {
            $.post("/api/shop/checkOrder", {id: id}, function (res) {
                if (typeof res === 'string') {
                    res = JSON.parse(res);
                }
                if (res.data == 1) {
                    window.location.href="/mobile/order-detail?id=" + id + "&order_type=" + order_type;
                }
            });
        }
        checkOrder()

        // 生成二维码
        function makeCode (url) {
            var qrcode = new QRCode('img');
            qrcode.makeCode(url, {
                width: 256,
                height: 256
            });//更换二维码内容
        }

        //支付开始
        function doPay () {
            var pay_way = $('input:radio[name="pay_way"]:checked').val();
            var data = {};
            data['pay_way'] = pay_way;
            data['order_id'] = id;
            if (pay_way === 'zhanglitong') {
                //创建支付文本框
                $.prompt({
                    title: '请输入支付密码',
                    text: ' ',
                    input: {
                        type: 'password'
                    },
                    empty: false, // 是否允许为空
                    onOK: function (input) {
                        //点击确认
                    },
                    onCancel: function () {
                        //点击取消
                    }
                });
                $("#weui-prompt-input").remove(); //去除原始输入框
                $(".weui-dialog__bd").append(' <input type="password" maxlength="6" class="weui-input weui-prompt-input" id="weui-prompt-input" value="">');
                data['pay_port'] = 'web';
                $("#weui-prompt-input").keyup(function () {
                    console.log("val", $(this).val());
                    var value =  $(this).val(),
                        len = value.length;
                    if (len >= 6) {
                        data['payword'] = value;
                        console.log("value", value);
                        postPay(data);
                    }
                });
            } else if (pay_way === 'wechat') {
                //console.log('我要微信支付');
                // 移动端
                if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
                    // 在手机 微信浏览器使用微信支付
                    var isWb = isWechatBrowser();
                    if (isWb) {
                        //console.log('是用手机微信');
                        $("#winfo").submit();
                    } else {
                        //console.log('是用手机浏览器');
                        $("#info").submit();
                    }
                } else {
                    //console.log("pc前几天");
                    data['pay_port'] = 'pc';
                    postPay(data);
                }

            } else if (pay_way === 'alipay') {
                // 在手机浏览器使用支付宝支付
                data['pay_port'] = 'web';
                // 在PC使用支付宝支付 扫码
                //console.log("我要支付宝支付");
                $("#ainfo").submit();
            }
        }

        //支付请求
        function postPay (data) {
            console.log("data", data);
            $.post("/api/pay", data, function (r) {
                if (typeof r === 'string') {
                    r = JSON.parse(r);
                }
                $.toast(r.info, 'text');
                if (r.port_type === 'pc') {
                    //生成之前先清空
                    makeCode('');
                    makeCode.clear();
                    // 二维码 r.data
                    makeCode(r.data);
                    // 设置提示信息
                    if (data['pay_way'] === 'wechat') {
                        $("#code .info").text('请用微信扫描上方二维码');
                    } else {
                        $("#code .info").text('请用支付宝扫描上方二维码');
                    }
                    // 显示二维码
                    $("#code").popup();
                } else {
                    checkOrder();
                }

            });
        }
    </script>
@endsection

@extends('layouts/footer')
